# instalacao do qt, e outras infos em
# http://cran.r-project.org/web/packages/qtbase/index.html
library (qtbase)


printTextEdit <- function() {
  print(textedit$toPlainText())
}
insertNewWord <- function() {
  textedit$setPlainText(paste(textedit$toPlainText(), "Hello", sep=" "))
}
correctLastWord <- function() {
  x = strsplit(textedit$toPlainText(), "[[:space:]]+")
  x[[1]][length(x[[1]])] <- "mimimi"
  textedit$setPlainText(paste(x[[1]], collapse=' '))
}

setButtonText <- function() {
  button4$setText("text")
}
textChanged <- function() {
  print("mudou texto")
}


widget <- Qt$QWidget()
widget$setWindowTitle("Word Predictor")
widget$setFixedWidth(600)
widget$setFixedHeight(220)

layout <- Qt$QGridLayout(widget)


textedit <- Qt$QPlainTextEdit()
textedit$setFixedWidth(580)
textedit$setFixedHeight(170)
textedit$updateGeometry()
layout$addWidget(textedit, 0, 0)
qconnect(textedit, "textChanged", textChanged)


button1 <- Qt$QPushButton("Print text")
qconnect(button1, "pressed", printTextEdit)
layout$addWidget(button1, 1, 0)

button2 <- Qt$QPushButton("Insert Hello")
qconnect(button2, "pressed",insertNewWord)
layout$addWidget(button2, 1, 1)

button3 <- Qt$QPushButton("Change word to Abc")
qconnect(button3, "pressed", correctLastWord)
layout$addWidget(button3, 1, 2)

button4 <- Qt$QPushButton("Change this text")
qconnect(button4, "pressed", setButtonText)
layout$addWidget(button4, 1, 3)

widget$show()



